import { Body, Controller, Get, Inject, Param, Post } from '@nestjs/common';
import { NotifyService } from './notify.service';
import { GetUserAgent, GetUserIp } from 'src/utils/GetUserMessTool';
import { SecretTool } from 'src/utils/SecretTool';
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { BasicVo } from 'src/pojo/vo/basic.vo';
import { CaptchaVo } from 'src/pojo/vo/captcha.vo';
import { SmsDto } from 'src/pojo/dto/sms.dto';

@ApiTags('通知模块')
@Controller('notify/v1')
export class NotifyController {
  @Inject()
  private readonly secretTool: SecretTool;

  @Inject()
  private readonly notifyService: NotifyService;

  @ApiOperation({ summary: '获取图形验证码' })
  @ApiParam({
    name: 'type',
    type: String,
    description: 'register:注册 login:登录',
    required: true,
  })
  @ApiResponse({
    status: 200,
    type: CaptchaVo,
    description: 'data数据',
  })
  @ApiResponse({
    status: 201,
    description: '通用数据',
    type: BasicVo,
  })
  @Get('captcha/:type')
  captcha(
    @Param('type') type: string,
    @GetUserIp() ip: string,
    @GetUserAgent() agent: string,
  ): Promise<CaptchaVo> {
    const key = this.secretTool.getSecret(ip + agent);
    return this.notifyService.captcha(key, type);
  }

  @ApiOperation({ summary: '获取短信验证码' })
  @ApiResponse({
    status: 201,
    description: '通用数据',
    type: BasicVo,
  })
  @Post('sendSmsCode')
  sendSmsCode(
    @Body() smsDto: SmsDto,
    @GetUserIp() ip: string,
    @GetUserAgent() agent: string,
  ) {
    const key = this.secretTool.getSecret(ip + agent);
    return this.notifyService.sendSmsCode(smsDto, key);
  }
}
